package com.example.opengldemo._111_;

import android.opengl.GLES30;
import android.view.View;

import com.example.opengldemo.util.ShaderBuffer;
import com.example.opengldemo.util.ShaderHandle;
import com.example.opengldemo.util.ShaderUtil;

public class Desert {
    public ShaderHandle shaderHandle ;
    public ShaderBuffer shaderBuffer ;

    public String vertexPath  = "lltest/desert_vertex.sh";
    public String fragmentPath  = "lltest/desert_frag.sh";

    public Desert(View view){
        shaderHandle = new ShaderHandle(
                ShaderUtil.createProgram(
                        ShaderUtil.loadFromAssetsFile(vertexPath,view.getResources()) ,
                        ShaderUtil.loadFromAssetsFile(fragmentPath,view.getResources())));
        shaderBuffer = new ShaderBuffer() ;
        shaderBuffer.setVertexBuffer(getVertexArray());
        shaderBuffer.setTexcoorBuffer(getTexcoorArray());
    }

    private float[] getTexcoorArray() {
        float[] array = new float[]{
                0f ,0f ,
                1f , 0f ,
                0f ,1f ,
                1f, 1f
        };
        return array ;
    }

    private float[] getVertexArray() {
        float unit = 0.9f ;
        float[] floats = new float[]{
                -unit , unit , 0 ,
                unit , unit , 0 ,
                -unit , -unit , 0 ,
                unit , -unit , 0
        } ;
        shaderBuffer.vCount = floats.length/3 ;
        return floats ;
    }
    public void drawSelf(int id){
        shaderHandle.program()
                .bindVertexHandle(shaderBuffer.getVertexBuffer())
                .bindTexCoorsHandle(shaderBuffer.getTexcoorBuffer())
                .bindMVPMaxtrixHandle()
                .bindDefaultTextureHandle(id)
                .run(GLES30.GL_TRIANGLE_STRIP , shaderBuffer.vCount);
    }
}
